/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.web.settlement;

import com.server.common.config.Global;
import com.server.common.persistence.Page;
import com.server.common.utils.StringUtils;
import com.server.common.web.BaseController;
import com.server.modules.sys.common.StaticDefine;
import com.server.modules.sys.entity.loan.CustomPay;
import com.server.modules.sys.entity.settlement.Settlement;
import com.server.modules.sys.entity.settlement.item.SettlementItem;
import com.server.modules.sys.service.loan.CustomPayService;
import com.server.modules.sys.service.settlement.SettlementService;
import com.server.modules.sys.utils.AmountUtils;
import com.server.modules.sys.utils.date.DateStyle;
import com.server.modules.sys.utils.date.DateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 结算总表Controller
 * @author 丁光彪
 * @version 2016-11-21
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/settlement/settlement")
public class SettlementController extends BaseController {

	@Autowired
	private SettlementService settlementService;

	@Autowired
	private CustomPayService customPayService;


	
	@ModelAttribute
	public Settlement get(@RequestParam(required=false) String id) {
		Settlement entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = settlementService.get(id);
		}
		if (entity == null){
			entity = new Settlement();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:settlement:settlement:view")
	@RequestMapping(value = {"list", ""})
	public String list(Settlement settlement, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Settlement> page = settlementService.findPage(new Page<Settlement>(request, response), settlement); 
		model.addAttribute("page", page);
		model.addAttribute("settlement", settlement);
		return "modules/sys/settlement/settlementList";
	}

	@RequiresPermissions("sys:settlement:settlement:view")
	@RequestMapping(value = "form")
	public String form(Settlement settlement, Model model) {
		model.addAttribute("settlement", settlement);

		return "modules/sys/settlement/settlementForm";
	}

	@RequiresPermissions("sys:settlement:settlement:edit")
	@RequestMapping(value = "save")
	public String save(Settlement settlement, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, settlement)){
			return form(settlement, model);
		}
		settlementService.save(settlement);
		addMessage(redirectAttributes, "保存结算总表成功");
		return "redirect:"+Global.getAdminPath()+"/sys/settlement/settlement/?repage";
	}
	
	@RequiresPermissions("sys:settlement:settlement:edit")
	@RequestMapping(value = "delete")
	public String delete(Settlement settlement, RedirectAttributes redirectAttributes) {
		settlementService.delete(settlement);
		addMessage(redirectAttributes, "删除结算总表成功");
		return "redirect:"+Global.getAdminPath()+"/sys/settlement/settlement/?repage";
	}


	@RequestMapping(value = "doSettlement")
	@ResponseBody
	public Object doSettlement(Settlement settlement,  String  customPayId,String settlementDateStr) throws Exception {
		if(StringUtils.isBlank(customPayId)){
			return false;
		}
		if(StringUtils.isNotBlank(settlementDateStr)){
            Date date = DateUtil.StringToDate(settlementDateStr, DateStyle.YYYY_MM_DD);
            settlement.setSettlementDate(date);
        }else{
            settlement.setSettlementDate(new Date());
        }
        CustomPay customPay= customPayService.get(new CustomPay(customPayId));
        settlement.setRemarks("财务结算");
		settlement.setOrders(new Date().getTime() + StringUtils.createOrder(7));
        settlement.setMemberId(customPay.getMemberid());
		settlement.setCustomPayId(customPayId);
		settlement.setType(StaticDefine.SETTLEMENT_TYPE.SETTLEMENT);
        customPay.setOrderFlag(StaticDefine.LOAN_CUSTOM_PAY_ORDER_FLAG.WATTINGPAYSETTLEMENT);
        customPayService.save(customPay);
        List<SettlementItem> items = settlement.getItems();
		Double amountIn=0d;
        Double amountOut=0d;
        for (SettlementItem item : items) {
			item.setRemarks("财务结算");
            item.setPayFlag(StaticDefine.SETTLEMENT_ITEM_PAY_FLAG.UNPAY);
            String amountStr = item.getAmountStr();
            String type = item.getType();
            if(StaticDefine.SETTLEMENT_ITEM_TYPE.IN.equals(type)){
                amountIn+= Double.parseDouble(amountStr) ;
            }else if(StaticDefine.SETTLEMENT_ITEM_TYPE.OUT.equals(type)){
                amountOut+= Double.parseDouble(amountStr) ;
            }
            item.setAmount(Long.parseLong(AmountUtils.changeY2F(amountStr)));
        }
		settlement.setAmountIn(Long.parseLong(AmountUtils.changeY2F(amountIn+"")) );
        settlement.setAmountOut(Long.parseLong(AmountUtils.changeY2F(amountOut+"")) );
        return settlementService.saveCascade(settlement);
	}

}